/**
 * Development Solutions (DS) Software
 * Created on 12-may-2011
 * @author Fernando Gil
 * lobho.gil@gmail.com
 *
 * Last Modified 16-may-2011
 * By: Fernando Gil
 *
 */

package View;

import java.awt.event.KeyEvent;
import java.util.StringTokenizer;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


public class SubnetFrame extends javax.swing.JFrame {
    private int bits,bitsNet;
    private String num1="",num2="",num3="",num4="";
    private About ab;
    private ImageIcon icQuestion;
    private String options[]={"No","Yes"};
    private String option[]={"OK"};

    /** Creates new form SubnetFrame */
    public SubnetFrame() {
        try{
           javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e){}
        initComponents();
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/Images/DSSoftware.png")).getImage());
        icQuestion = new ImageIcon(getClass().getResource("/Images/DSSQuestion48.png"));
        this.setLocationRelativeTo(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jTClass = new javax.swing.JTextField();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jTBinAdd = new javax.swing.JTextField();
        jTBinMask = new javax.swing.JTextField();
        jTMask = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jCSubnetBits = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jCMaskBits = new javax.swing.JComboBox();
        jCMaxSubnets = new javax.swing.JComboBox();
        jCMaxHosts = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Subnet Calculator");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(221, 217, 210));

        jPanel2.setBackground(new java.awt.Color(221, 217, 210));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Network Address"));

        jTClass.setEditable(false);
        jTClass.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTClass.setText("Class");
        jTClass.setFocusable(false);

        try {
            jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#**.#**.#**.0")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jFormattedTextField1.setText("000.000.000.0");
        jFormattedTextField1.setFont(new java.awt.Font("Lucida Grande", 0, 14));
        jFormattedTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jFormattedTextField1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jFormattedTextField1FocusLost(evt);
            }
        });
        jFormattedTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jFormattedTextField1KeyTyped(evt);
            }
        });

        jTBinAdd.setEditable(false);
        jTBinAdd.setText("Binary Address");
        jTBinAdd.setFocusable(false);

        jTBinMask.setEditable(false);
        jTBinMask.setText("Binary Mask");
        jTBinMask.setFocusable(false);

        jTMask.setEditable(false);
        jTMask.setFont(new java.awt.Font("Lucida Grande", 0, 14));
        jTMask.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTMask.setText("000.000.000.0");
        jTMask.setToolTipText("Network Mask");
        jTMask.setFocusable(false);

        jPanel4.setBackground(new java.awt.Color(221, 217, 210));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Subnet"));

        jCSubnetBits.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCSubnetBitsActionPerformed(evt);
            }
        });

        jLabel1.setText("Subnet bits");

        jLabel2.setText("Mask bits");

        jLabel3.setText("Max subnets");

        jLabel4.setText("Max hosts");

        jCMaskBits.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCMaskBitsActionPerformed(evt);
            }
        });

        jCMaxSubnets.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCMaxSubnetsActionPerformed(evt);
            }
        });

        jCMaxHosts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCMaxHostsActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(9, 9, 9)
                        .add(jLabel1)
                        .add(40, 40, 40)
                        .add(jLabel2)
                        .add(52, 52, 52)
                        .add(jLabel3)
                        .add(31, 31, 31)
                        .add(jLabel4))
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jCSubnetBits, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jCMaskBits, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 3, Short.MAX_VALUE)
                        .add(jCMaxSubnets, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 111, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jCMaxHosts, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 113, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jCSubnetBits, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jCMaskBits, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jCMaxSubnets, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jCMaxHosts, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jButton1.setText("Calculate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jTClass)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2Layout.createSequentialGroup()
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(jTMask)
                                    .add(jFormattedTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE))
                                .add(18, 18, 18)
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jTBinAdd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jTBinMask, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(34, 34, 34)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jButton1)
                            .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jTClass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jFormattedTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jTBinAdd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(6, 6, 6)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTMask, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jTBinMask, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(8, 8, 8)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMenu1.setText("Subnet Calculator");

        jMenuItem3.setText("About");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem2.setText("Exit");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Botón para clacular*/
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        bitsNet = Integer.parseInt(this.jCSubnetBits.getSelectedItem().toString() );
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Subnets(num1,num2,num3,bits,bitsNet).setVisible(true);
            }
        });

    }//GEN-LAST:event_jButton1ActionPerformed

    /** Focus Gainde*/
    private void jFormattedTextField1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField1FocusGained
        this.jFormattedTextField1.selectAll();     
    }//GEN-LAST:event_jFormattedTextField1FocusGained

    /** Typed, para no admitir otros caracteres queno  sean numeros*/
    private void jFormattedTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFormattedTextField1KeyTyped
        int k=(int)evt.getKeyChar();
        if ( (k<48 || k>57)){
             evt.setKeyChar((char)KeyEvent.VK_CLEAR);
        }
    }//GEN-LAST:event_jFormattedTextField1KeyTyped

    /** Al salir del cuadro de texto donde se ha ingresado la IP, se procede a determinar
     la máscara de bitSub, la clase, la mascara de bitSub en bits y la bitSub en bits*/
    private void jFormattedTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField1FocusLost
        String ip = jFormattedTextField1.getText(); //Guardar la ip del txt
        StringTokenizer st = new StringTokenizer(ip,"."); //separa los numeros por el .
        String ipClass = null,mask = null,binMask = null;
        int max=0; //numero máximo de bits que puede abarcar la porcion de host o de bitSub
                    // el mínimo siempre es 2.
        try{
            /*Quitar todos los espacios a cada numero*/
           num1 = removeAllSubstring(st.nextToken()," ");
           num2 = removeAllSubstring(st.nextToken()," ");
           num3 = removeAllSubstring(st.nextToken()," ");
           num4 = removeAllSubstring(st.nextToken()," ");
        }catch(Exception ex){
            System.out.println("Error en removeAllSubstring");
            /*System.out.println("1: "+num1);
            System.out.println("2: "+num2);
            System.out.println("3: "+num3);
            System.out.println("4: "+num4);*/
        }
        try{   
           /*Verificar que los numeros sean menores a 255*/
           if(Integer.parseInt(num1)<1)
               num1="1";
           if(Integer.parseInt(num1)>223)
               num1="223";
           if(Integer.parseInt(num1)==127)
               num1="126";
           if(Integer.parseInt(num2)>254)
               num2="254";
           if(Integer.parseInt(num3)>254)
               num3="254";
           jFormattedTextField1.setText(fillZero(num1,3)+fillZero(num2,3)+fillZero(num3,3)+fillZero(num4,3));
        }catch(Exception ex){
            System.out.println("Error en Verificacin de numeros");
        }
        try{
           /** Obtener los valores binarios*/
           String bin1 = fillZero( Integer.toBinaryString( Integer.parseInt( num1 ) )  ,8 );
           String bin2 = fillZero( Integer.toBinaryString( Integer.parseInt( num2 ) )  ,8 );
           String bin3 = fillZero( Integer.toBinaryString( Integer.parseInt( num3 ) )  ,8 );
           String bin4 = fillZero( Integer.toBinaryString( Integer.parseInt( num4 ) )  ,8 );
           this.jTBinAdd.setText( bin1+"."+bin2+"."+bin3+"."+bin4);
    
           /*Encontrar la Máscara de Red y Clase de IP*/
           if(Integer.parseInt(num1)<127){
               ipClass="Class A  1.0.0.0 - 126.255.255.255 /8";
               mask="255.0.0.0";
               binMask="11111111.00000000.00000000.00000000";
               max = 22;
               bits = 8;
           }else if(Integer.parseInt(num1)>127 && Integer.parseInt(num1)<192 ){
               ipClass="Class B  128.0.0.0 - 191.255.255.255 /16";
               mask="255.255.0.0";
               binMask="11111111.11111111.00000000.00000000";
               max = 14;
               bits = 16;
           }else if(Integer.parseInt(num1)<224 ){
               ipClass="Class C  192.0.0.0 - 223.255.255.255 /24";
               mask="255.255.255.0";
               binMask="11111111.11111111.11111111.00000000";
               max= 6;
               bits= 24;
           }
        }catch(Exception ex){
            System.out.println("Error al Convertir a binario");
        }
        try{
           this.jTBinMask.setText(binMask);
           this.jTClass.setText(ipClass);
           this.jTMask.setText(mask);
           /*Llenar los Combos de acuerdo a la clase*/
           this.fillCombo(max,ipClass);
        }catch(Exception ex){
            System.out.println("Error al LLenar Combos");
        }
    }//GEN-LAST:event_jFormattedTextField1FocusLost

    private void jCMaskBitsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCMaskBitsActionPerformed
        if(this.jCSubnetBits.getItemCount()>0 &&
               this.jCMaskBits.getItemCount()>0 &&
               this.jCMaxSubnets.getItemCount()>0 &&
               this.jCMaxHosts.getItemCount()>0){
            int mask = bits+this.jCMaskBits.getSelectedIndex()+2;
            /* Con la máscara determino la porcion de Red*/
            int subnet = mask-bits;
            int pHost = 32-subnet;
            //System.out.println("Determiar subnet");
            this.jCSubnetBits.setSelectedItem(subnet);
            /** Con la porcion de Red determinar el numero de Subredes y de Hosts **/
         /*this.jCMaxSubnets.setSelectedItem(   (int)(Math.pow(2,subnet)-2)    );
            this.jCMaxHosts.setSelectedItem( (int)(Math.pow(2,pHost)-2) );*/
            System.out.println("MaskBits");
        }
    }//GEN-LAST:event_jCMaskBitsActionPerformed

    private void jCSubnetBitsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCSubnetBitsActionPerformed
       if(this.jCSubnetBits.getItemCount()>0 &&
               this.jCMaskBits.getItemCount()>0 &&
               this.jCMaxSubnets.getItemCount()>0 &&
               this.jCMaxHosts.getItemCount()>0){
        //System.out.println("subnet");
        int bitSub = this.jCSubnetBits.getSelectedIndex()+2;
        int pRed = bits+bitSub;
        int pHost = 32-pRed;
        //System.out.println("Porcion de red: "+pRed);
        //System.out.println("Porcion de host: "+pHost);
        /** Con la porcion de Red armada ahora determinamos la máscara **/
        this.jCMaskBits.setSelectedItem("/"+pRed);
        /** Con la porcion de Red determinar el numero de Subredes y de Hosts **/
        this.jCMaxSubnets.setSelectedItem(   (int)(Math.pow(2,bitSub)-2)    );
        this.jCMaxHosts.setSelectedItem( (int)(Math.pow(2,pHost)-2) );
        System.out.println("SubnetBits");
       }
    }//GEN-LAST:event_jCSubnetBitsActionPerformed

    private void jCMaxSubnetsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCMaxSubnetsActionPerformed
        if(this.jCSubnetBits.getItemCount()>0 &&
               this.jCMaskBits.getItemCount()>0 &&
               this.jCMaxSubnets.getItemCount()>0 &&
               this.jCMaxHosts.getItemCount()>0){
            int bitSub = this.jCMaxSubnets.getSelectedIndex() +2;
            this.jCSubnetBits.setSelectedItem(bitSub);
            /*int pRed = bits+bitSub;
            int pHost = 32-pRed;
            this.jCMaskBits.setSelectedItem("/"+pRed);
            this.jCMaxHosts.setSelectedItem( (int)(Math.pow(2,pHost)-2) );*/
            System.out.println("MaxSubnets");
        }
    }//GEN-LAST:event_jCMaxSubnetsActionPerformed

    private void jCMaxHostsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCMaxHostsActionPerformed
       if(this.jCSubnetBits.getItemCount()>0 &&
               this.jCMaskBits.getItemCount()>0 &&
               this.jCMaxSubnets.getItemCount()>0 &&
               this.jCMaxHosts.getItemCount()>0){
            int subnetBits = (this.jCMaxHosts.getSelectedIndex()+2);
            this.jCSubnetBits.setSelectedItem(subnetBits);
            System.out.println("MaxHosts");
        }
    }//GEN-LAST:event_jCMaxHostsActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        if(ab==null){
            Thread t = new Thread(
                    new Runnable() {
                        public void run() {
                           ab = new About();
                           ab.setVisible(true);
                           //work.paintPanel();
                        }
                    });
            t.start();
        }else{
            /* Primero reasigno la posicion del Frame en relacion al Menú
             y después lo hacemos Visivle.*/
            ab.setVisible(true);
            //work.paintPanel();
        }
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        int opc= JOptionPane.showOptionDialog(null, "Do you really want to exit? ","Warning",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,icQuestion,options,options[1]);
        if(opc==1)
            System.exit(0);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /** Función para lelnar los Combo Box con valores de acuerdo a la calse de al IP*/
    public void fillCombo(int max, String c){
       //System.out.println("Limpiar Combos");
        try{
           this.jCSubnetBits.removeAllItems();
           this.jCMaskBits.removeAllItems();
           this.jCMaxSubnets.removeAllItems();
           this.jCMaxHosts.removeAllItems();
        }catch(Exception ex){
            System.out.println("Error al Limpiar Combos");
        }
       //System.out.println("Combos Limpiios");
        //Llenar el Combo de Subnets bits
        int i=2; //Se inicia en 2 xq es el numero minimo de bits para subnet
        try{
            //System.out.println("LLenar Subnet");
            while(i<=max)
                this.jCSubnetBits.addItem(i++);
        }catch(Exception ex){
            System.out.println("Error al Llenar Subnet Bits");
        }
        //System.out.println("Subnet LLEna");
        //llenar el combo de mask Subnets
        try{
            if(c.equals("Class A  1.0.0.0 - 126.255.255.255 /8"))
                i=10;
            if(c.equals("Class B  128.0.0.0 - 191.255.255.255 /16"))
                i=18;
            if(c.equals("Class C  192.0.0.0 - 223.255.255.255 /24"))
                i=26;
            while(i<=30)
                this.jCMaskBits.addItem("/"+i++);
        }catch(Exception ex){
            System.out.println("Error al Llenar Mask Bits");
        }
        //Llenar los combos de Max Subnets y Mas Hosts
        try{
            i = 2;
            while(i<=max)
                this.jCMaxSubnets.addItem( (int)(Math.pow(2, i++)-2) );
            while(i>2)
                this.jCMaxHosts.addItem( (int)(Math.pow(2, --i)-2) );
        }catch(Exception ex){
            System.out.println("Error al Llenar Maxs");
        }

    }
    
    /** Función para agregar todos los ceros necesarios para que sean octetos*/
    public String fillZero(String bin, int size){
        while(bin.length()<size)
            bin="0"+bin;
        return bin+"";
    }

    /**Funcion para eliminar todos los substring (remove) de word*/
    private String removeAllSubstring(String word,String remove) {
        String acum=word;
        if(word.length()>remove.length()){
            int i=0;
            acum="";
            for(i=0;i<=word.length()-remove.length();i++){
               /*Armamos una cadena del tamaño de la palabra a remover*/
                String sub=word.substring(i, i+remove.length());
                //System.out.println(sub);
               /*Evaluar la cadena armada con la palabra que se busca remover*/
                if(!sub.equals(remove)){
                   acum += sub;
                }
            }
        }
        return acum;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCMaskBits;
    private javax.swing.JComboBox jCMaxHosts;
    private javax.swing.JComboBox jCMaxSubnets;
    private javax.swing.JComboBox jCSubnetBits;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField jTBinAdd;
    private javax.swing.JTextField jTBinMask;
    private javax.swing.JTextField jTClass;
    private javax.swing.JTextField jTMask;
    // End of variables declaration//GEN-END:variables

}
